// mine//pages/mine_order/index.js
const apis = require('../../../utils/api').apis;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataArr: [],
    state: "",
    pageNo: 1,
    active: "all",
    total:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  switchTab(e) {
    let type = e.currentTarget.dataset.type;
    if (type == 'all') {
      this.setData({
        active:type,
        state: "",
        pageNo: 1,
      })
    }else if(type == 'payment'){
      this.setData({
        active:type,
        state: "payment",
        pageNo: 1,
      })
    }else if(type == 'notpay'){
      this.setData({
        active:type,
        state: "notpay",
        pageNo: 1,
      })
    }
    this.getData()
  },
  getData() {
    wx.showLoading({
      title: '加载中',
    })
    apis.orderGetUserOrderList({
      state: this.data.state,
      pageNo: this.data.pageNo,
      pageSize: 10,
    }).then(res => {
      if(this.data.pageNo==1){
        this.setData({
          dataArr: res.data.records,
          total:res.data.total
        })
      }else{
        this.setData({
          dataArr:this.data.dataArr.concat(res.data.records),
          total:res.data.total
        })
      }
      wx.hideLoading()
    })
  },
  goDetail(e){
    let item=e.currentTarget.dataset.item;
    // let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/mine/pages/mine_order_detail/index?item='+JSON.stringify(item),
    })
  },
  // 取消订单
  cancelOrder(e){
    let id=e.currentTarget.dataset.id;
    apis.orderCancelOrder({
      orderId:id
    }).then(res=>{
      if(!res.success){
        wx.showToast({
          title: '操作失败，请稍后再试',
          icon:'none'
        })
        return
      }
      this.getData()
    })
  },
  // 立即购买
  buyOrder(e){
    let id=e.currentTarget.dataset.item.curriculumId;
    let curriculumForm=e.currentTarget.dataset.item.curriculumForm;
    let couponMoney=e.currentTarget.dataset.item.couponMoney;//优惠券
    let score=e.currentTarget.dataset.item.score;//学到币
    let orderNo=e.currentTarget.dataset.item.orderNo;//订单号
    let payMoney=e.currentTarget.dataset.item.payMoney;//支付金额
    console.log(id,e.currentTarget.dataset.item.curriculumForm,couponMoney,score)
    console.log(id)
    wx.navigateTo({
      url: '/curriculum/pages/purchase/index?id='+id+'&classification='+curriculumForm+'&couponMoney='+couponMoney+'&score='+score+'&orderNo='+orderNo+'&payMoney='+payMoney,
    })
  },
  goIndex(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  goCourse(e){
    let id=e.currentTarget.dataset.item.curriculumId;
    wx.navigateTo({
      url: '/curriculum/pages/course/index?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log(this.data.dataArr.length,this.data.total)
    if(this.data.dataArr.length<this.data.total){
      this.setData({
        pageNo: ++this.data.pageNo
      })
      this.getData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})